package com.liming.order.controller;

import com.liming.common.pojo.dto.order.user.UserOrderDTO;
import com.liming.common.pojo.dto.order.user.mini.MiniOrderQueryPage;
import com.liming.common.pojo.vo.PageVO;
import com.liming.common.pojo.vo.order.OrderVO;
import com.liming.common.pojo.vo.order.mini.MiniOrderItemVO;
import com.liming.common.pojo.vo.order.mini.MiniOrderVO;
import com.liming.common.result.LMResult;
import com.liming.order.service.OrderItemMircoService;
import com.liming.order.service.OrderMircoService;
import com.liming.service.wx.pojo.vo.PrepayVO;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author liming
 * @date 2025/1/2
 */
@RestController
@AllArgsConstructor
@RequestMapping("/service/user-order")
public class UserOrderMircoController {

    private final OrderMircoService orderMircoService;
    @Lazy
    private final OrderItemMircoService orderItemMircoService;

    @Operation(summary = "根据订单ID获取订单详情")
    @GetMapping("{id}")
    public LMResult<OrderVO> detailById(@PathVariable Long id) {
        return LMResult.success(orderMircoService.detailById(id));
    }

    @Operation(summary = "用户所有状态的订单数量")
    @GetMapping("allStatusCount/{userId}")
    public LMResult<Map<Integer, Integer>> allStatusCount(@PathVariable Long userId) {
        return LMResult.success(orderMircoService.allStatusCount(userId));
    }

    @Operation(summary = "分页 - 用户订单")
    @GetMapping
    public LMResult<PageVO<MiniOrderVO>> page(MiniOrderQueryPage queryPage) {
        return LMResult.success(orderMircoService.miniPage(queryPage));
    }

    @Operation(summary = "根据订单ID获取订单商品列表")
    @GetMapping("itemsById/{id}")
    public LMResult<List<MiniOrderItemVO>> itemsById(@PathVariable Long id) {
        return LMResult.success(orderItemMircoService.itemsByOrderId(id));
    }

    @Operation(summary = "用户下单")
    @PostMapping("createOrder")
    public LMResult<PrepayVO> createOrder(@RequestBody UserOrderDTO userOrderDTO) {
        return LMResult.success(orderMircoService.createOrder(userOrderDTO));
    }

    @Operation(summary = "根据订单ID列表获取商品ID列表")
    @PostMapping("productIdListByOrderIdList")
    public LMResult<List<Long>> productIdListByOrderIdList(@RequestBody List<Long> keyIdList) {
        return LMResult.success(orderMircoService.productIdListByOrderIdList(keyIdList));
    }

    @Operation(summary = "取消订单 根据订单ID")
    @PutMapping("cancel/{id}")
    public LMResult<Boolean> cancel(@PathVariable Long id) {
        return LMResult.success(orderMircoService.cancel(id));
    }

    @Operation(summary = "完成订单 根据订单ID")
    @PutMapping("complete/{id}")
    public LMResult<Boolean> complete(@PathVariable Long id) {
        return LMResult.success(orderMircoService.complete(id));
    }

    @Operation(summary = "订单支付成功 调用（回调）接口")
    @PutMapping("createOrderPaySuccess/{id}")
    public LMResult<Boolean> createOrderPaySuccess(@PathVariable Long id) {
        return LMResult.success(orderMircoService.createOrderPaySuccess(id));
    }
}
